var subClass = "";

$(document).ready(function(){
    subClass = request("subClass");
    if ('' == subClass) {
        Commonjs.alert('分类参数不正确',false);
        return;
    }

    vmDiyProductIndex.getProductList();
});


function isNull(str){
	if(str==null || str==undefined)
		return "";
	return str;
}

var vmDiyProductIndex = new Vue({
    el: '#main-block',
    data: function() {
        return {
            linkRoot: '',
			isDiy: false,
            diyPage: '',
            diyHeight: 400,
            category: {
                categoryName: ''
			},
            productName:'',
			proList: [],
            page: {
                total: 0,
                page: 1,
                size: 9
            }
        }
    },
    methods: {
		loadInfo: function (category, proList, rsp) {
		    var self = this;
            vmDiyProductIndex.isDiy = category.diyPage != '';
            vmDiyProductIndex.category = category;
            vmDiyProductIndex.proList = proList;

            if (vmDiyProductIndex.isDiy) {
                vmDiyProductIndex.diyPage = category.diyPage;
                vmDiyProductIndex.diyHeight = category.diyHeight;

                //添加监听
                var cloudMessage = new CloudMessage();
                cloudMessage.addListen('diyProductQuery', function (message) {
                    var messageType = 'diyProductQueryResponse';
                    if (message.page == vmDiyProductIndex.page.page) {
                        var data = rsp;
                        delete data.code;
                        delete data.msg;
                        delete data.result;
                        data.linkRoot = vmDiyProductIndex.linkRoot;
                        cloudMessage.send(document.getElementById('diyForm'), messageType, data);
                    } else {
                        vmDiyProductIndex.page.page = message.page;
                        var service = {};
                        service.productClass = 'diy';
                        service.subClass = subClass;
                        service.page = message.page;
                        service.pageSize=self.page.size;
                        var fn="getCustomerProductList";
                        service = Commonjs.jsonToString(service);
                        var params = Commonjs.getParams(fn,service);//获取参数

                        $.ajax({
                            datatype:"json",
                            type:"POST",
                            url: weburl,
                            data:params,
                            cache : false,
                            success: function(data){
                                var result = JSON.parse(data);
                                delete result.code;
                                delete result.msg;
                                delete result.result;
                                result.linkRoot = vmDiyProductIndex.linkRoot;
                                cloudMessage.send(document.getElementById('diyForm'), messageType, result);
                            }
                        });
                    }
                });
                cloudMessage.startListen(Commonjs.getHostUrlFromString(category.diyPage));
            }
        },

        /**
         * 获取产品列表.
         */
        getProductList: function () {
            var service = {};
            service.productClass = 'diy';
            service.subClass = subClass;
            service.page = this.page.page;
            service.pageSize = this.page.size;
            service.productName = this.productName;
            var fn="getCustomerProductList";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);//获取参数

            $.ajax({
                datatype:"json",
                type:"POST",
                url: weburl,
                data:params,
                cache : false,
                success: function(data){
                    var rsp = JSON.parse(data);
                    vmDiyProductIndex.page.total = rsp.rows;
                    vmDiyProductIndex.loadInfo(rsp.category, rsp.data, rsp);
                }
            });
        },
        getProductListSerch: function () {
            this.page.page = 1;
            this.getProductList();
        },
        /**
         * 分页变化.
         * @param page
         */
        changePage: function (page) {
            this.page.page = page;
            this.getProductList();
        }
    },
    mounted: function () {
    	this.linkRoot = Commonjs.getCfgVal(configParam.common.cfgKey.template)  + configParam.htmlPage.diyDetail + '?productCode=';
    }
});
